import * as echarts from 'echarts'

export const useECharts = (chartRef: Ref<HTMLDivElement | undefined, HTMLDivElement | undefined>, options: ECOption) => {
    let myCharts: echarts.ECharts

    const handleResize = () => {
        if (myCharts) {
            myCharts.resize()
        }
    }

    onMounted(() => {
        if (chartRef.value) {
            myCharts = echarts.init(chartRef.value)
            myCharts.setOption(options)
            window.addEventListener('resize', handleResize)
        }
    })

    const getCarts = () => {
        if (myCharts) {
            return myCharts
        }
    }

    onUnmounted(() => {
        if (myCharts) {
            window.removeEventListener('resize', handleResize)
            // 销毁 ECharts 实例
            myCharts.dispose()
        }
    })
    return { getCarts }
}
